package com.yaodian100.mall.shopping.web;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.ec.delivery.entity.DeliveryRule;
import com.yaodian100.ec.delivery.entity.ViewDeliveryRule;
import com.yaodian100.ec.delivery.service.DeliveryRuleService;

public class AddressDataAction extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8747676543156203457L;
	private final Logger logger = LoggerFactory.getLogger(AddressDataAction.class);
	private String province;
	private String city;
	private String district;
	private String actionResult;
	private String id;
	private String type;
	private String service;
	private Object[] provinces;
	private Object[] citys;
	private Object[] districts;
	@Autowired
	private DeliveryRuleService deliverRuleService;

	@Override
	public void prepare() throws Exception {
		clearErrorsAndMessages();
	}

	/**
	 * 依購物車商品查询符合的物流配送城市
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findCartProvinces() throws Exception {
		Map<String, String> provinces = deliverRuleService.getCartProvinces(id);
		actionResult = combineProvinces(provinces);
		return "jsonGetProvinces";
	}

	/**
	 * 依訂單商品查询符合的物流配送城市
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findOrderProvinces() throws Exception {
		Map<String, String> provinces = deliverRuleService.getOrderProvinces(id);
		actionResult = combineProvinces(provinces);
		return "jsonGetCitys";
	}

	/**
	 * 依商品編號查询符合的物流配送城市
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findProductProvinces() throws Exception {
		Map<String, String> provinces = deliverRuleService.getProductProvinces(id);
		actionResult = combineProvinces(provinces);
		return "jsonGetProvinces";
	}

	/**
	 * 依訂單商品查询符合的物流配送城市-退换货用
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findServiceOrderProvinces() throws Exception {
		Map<String, String> provinces = deliverRuleService.getServiceOrderProvinces(id);
		actionResult = combineProvinces(provinces);
		return "jsonGetCitys";
	}

	String combineProvinces(Map<String, String> provinces) {
		if (provinces.size() > 0) {
			actionResult = StringUtils.defaultString(actionResult);
			for (Map.Entry<String, String> province : provinces.entrySet()) {
				actionResult = actionResult + province.getKey() + "," + province.getValue() + "$";
			}
			if (actionResult.length() > 0) {
				actionResult = actionResult.substring(0, actionResult.length() - 1);
			}
		}
		return actionResult;
	}

	/**
	 * 依購物車商品查询符合的物流配送城市
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findCartCitys() throws Exception {
		if (StringUtils.isNotBlank(province)) {
			Map<String, String> citys = deliverRuleService.getCartCitys(id, province);
			actionResult = combineCitys(citys);
		}
		return "jsonGetCitys";
	}

	/**
	 * 依訂單商品查询符合的物流配送城市
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findOrderCitys() throws Exception {
		if (StringUtils.isNotBlank(province)) {
			Map<String, String> citys = deliverRuleService.getOrderCitys(id, province);
			actionResult = combineCitys(citys);
		}
		return "jsonGetCitys";
	}

	/**
	 * 依商品編號查询符合的物流配送城市
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findProductCitys() throws Exception {
		if (StringUtils.isNotBlank(province)) {
			Map<String, String> citys = deliverRuleService.getProductCitys(id, province);
			actionResult = combineCitys(citys);
		}
		return "jsonGetCitys";
	}

	/**
	 * 依訂單商品查询符合的物流配送城市-退换货用
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findServiceOrderCitys() throws Exception {
		if (StringUtils.isNotBlank(province)) {
			Map<String, String> citys = deliverRuleService.getServiceOrderCitys(id, province);
			actionResult = combineCitys(citys);
		}
		return "jsonGetCitys";
	}

	String combineCitys(Map<String, String> citys) {
		if (citys.size() > 0) {
			actionResult = StringUtils.defaultString(actionResult);
			for (Map.Entry<String, String> city : citys.entrySet()) {
				actionResult = actionResult + city.getKey() + "," + city.getKey() + "$";
			}
			if (actionResult.length() > 0) {
				actionResult = actionResult.substring(0, actionResult.length() - 1);
			}
		}
		return actionResult;
	}

	// public String findCitys() throws Exception {
	// if (StringUtils.isNotBlank(province)) {
	// CommonCriteria cri = new CommonCriteria();
	// cri.addEq("name", province);
	// if (addressService.getList(0, -1, cri, null).size() > 0) {
	// int provinceId = addressService.getList(0, -1, cri, null).get(0).getId().intValue();
	// cri = new CommonCriteria();
	// cri.addEq("provinceId", provinceId);
	// List<City> citys = addressService.getCityList(cri);
	// actionResult = StringUtils.defaultString(actionResult);
	// for (City city : citys) {
	// actionResult = actionResult + city.getId() + "," + city.getName() + "$";
	// }
	// if (actionResult.length() > 0) {
	// actionResult = actionResult.substring(0, actionResult.length() - 1);
	// }
	// }
	// }
	// return "jsonGetCitys";
	// }

	public String findCity() throws Exception {
		return "findCity";
	}

	/**
	 * 依購物車商品查询符合的物流配送地區(for cod)
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findCartDistrictsCod() throws Exception {
		if (StringUtils.isNotBlank(city)) {
			List<ViewDeliveryRule> districts = deliverRuleService.getCartDistricts(id, city);
			actionResult = combineDistrictsCod(districts);
		}
		return "jsonGetDistrictsCod";
	}

	/**
	 * 依訂單商品查询符合的物流配送地區(for cod)
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findOrderDistrictsCod() throws Exception {
		if (StringUtils.isNotBlank(city)) {
			List<ViewDeliveryRule> districts = deliverRuleService.getOrderDistricts(id, city);
			actionResult = combineDistrictsCod(districts);
		}
		return "jsonGetDistrictsCod";
	}

	String combineDistrictsCod(List<ViewDeliveryRule> districts) {
		if (null != districts && !districts.isEmpty()) {
			actionResult = StringUtils.defaultString(actionResult);
			for (ViewDeliveryRule district : districts) {
				actionResult = actionResult + district.getToDistrict() + ","
						+ (StringUtils.equals("Y", district.getIsCod()) ? "※" : "") + district.getToDistrict() + ","
						+ district.getIsPos() + "," + district.getVersion() + "," + district.getType() + "$";
			}
			if (actionResult.length() > 0) {
				actionResult = actionResult.substring(0, actionResult.length() - 1);
			}
		}
		return actionResult;
	}

	// public String findDistrictsCod() throws Exception {
	// if (StringUtils.isNotBlank(city)) {
	// CommonCriteria cri = new CommonCriteria();
	// cri.addEq("name", city);
	// if (addressService.getCityList(cri).size() > 0) {
	// int cityId = addressService.getCityList(cri).get(0).getId().intValue();
	// cri = new CommonCriteria();
	// cri.addEq("cityId", cityId);
	// List<District> districts = addressService.getDistrictList(cri);
	// actionResult = StringUtils.defaultString(actionResult);
	// for (District district : districts) {
	// if (district.getIsCod().equals("Y")) {
	// actionResult = actionResult + district.getId() + ",※" + district.getName() + "," + district.getIsPos()
	// + "," + district.getServiceSupport() + "," + district.getNonServiceSupport() + "$";
	// } else {
	// actionResult = actionResult + district.getId() + "," + district.getName() + "," + district.getIsPos() + "$";
	// }
	// }
	// if (actionResult.length() > 0) {
	// actionResult = actionResult.substring(0, actionResult.length() - 1);
	// }
	// }
	// }
	// return "jsonGetDistrictsCod";
	// }

	/**
	 * 依商品類別、省市區、服務選項查詢可服務/不可服務區域
	 * 
	 * @return
	 */
	public String findCartServiceSupport() {
		actionResult = StringUtils.defaultString(actionResult, "");
		if (StringUtils.isNotBlank(id) && StringUtils.isNotBlank(province) && StringUtils.isNotBlank(city)
				&& StringUtils.isNotBlank(district) && StringUtils.isNotBlank(service)) {
			String[] services = new String[] { "10", "20", "30" };
			if (ArrayUtils.contains(services, service)) {
				DeliveryRule dr = deliverRuleService.getCartDeliveryRule(id, province, city, district, service);
				if (StringUtils.isNotBlank(dr.getDeliverySupportArea())
						|| StringUtils.isNotBlank(dr.getDeliveryNoneSupportArea())) {
					actionResult = StringUtils.defaultString(dr.getDeliverySupportArea(), "") + "|"
							+ StringUtils.defaultString(dr.getDeliveryNoneSupportArea(), "");
				} else {
					actionResult = "";
				}
			}
		}
		return "jsonGetServiceSupport";
	}

	/**
	 * 依購物車商品查询符合的物流配送地區(一般)
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findCartDistricts() throws Exception {
		if (StringUtils.isNotBlank(city)) {
			List<ViewDeliveryRule> districts = deliverRuleService.getCartDistricts(id, city);
			actionResult = combineDistricts(districts);
		}
		return "jsonGetDistricts";
	}

	/**
	 * 依訂單商品查询符合的物流配送地區(一般)
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findOrderDistricts() throws Exception {
		if (StringUtils.isNotBlank(city)) {
			List<ViewDeliveryRule> districts = deliverRuleService.getOrderDistricts(id, city);
			actionResult = combineDistricts(districts);
		}
		return "jsonGetDistricts";
	}

	/**
	 * 依商品編號查询符合的物流配送地區(一般)
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findProductDistricts() throws Exception {
		if (StringUtils.isNotBlank(city)) {
			List<ViewDeliveryRule> districts = deliverRuleService.getProductDistricts(id, city);
			actionResult = combineDistricts(districts);
		}
		return "jsonGetDistricts";
	}

	/**
	 * 依訂單商品查询符合的物流配送地區(一般)-退换货用
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findServiceOrderDistricts() throws Exception {
		if (StringUtils.isNotBlank(city)) {
			List<ViewDeliveryRule> districts = deliverRuleService.getServiceOrderDistricts(id, city);
			actionResult = combineDistricts(districts);
		}
		return "jsonGetDistricts";
	}

	String combineDistricts(List<ViewDeliveryRule> districts) {
		if (null != districts && !districts.isEmpty()) {
			actionResult = StringUtils.defaultString(actionResult);
			for (ViewDeliveryRule district : districts) {
				actionResult = actionResult + district.getToDistrict() + "," + district.getToDistrict() + ","
						+ district.getVersion() + "$";
			}
			if (actionResult.length() > 0) {
				actionResult = actionResult.substring(0, actionResult.length() - 1);
			}
		}
		return actionResult;
	}

	// public String findDistricts() throws Exception {
	// if (StringUtils.isNotBlank(city)) {
	// CommonCriteria cri = new CommonCriteria();
	// cri.addEq("name", city);
	// if (addressService.getCityList(cri).size() > 0) {
	// int cityId = addressService.getCityList(cri).get(0).getId().intValue();
	// cri = new CommonCriteria();
	// cri.addEq("cityId", cityId);
	// List<District> districts = addressService.getDistrictList(cri);
	// actionResult = StringUtils.defaultString(actionResult);
	// for (District district : districts) {
	// actionResult = actionResult + district.getId() + "," + district.getName() + "$";
	// }
	// if (actionResult.length() > 0) {
	// actionResult = actionResult.substring(0, actionResult.length() - 1);
	// }
	// }
	// }
	// return "jsonGetDistricts";
	// }
	/**
	 * 查询大材积支援的省份
	 * 
	 * @return
	 * @throws CoreException
	 */
	public String findOtherDeliveryRuleProvinces() throws Exception {
		provinces = deliverRuleService.findOtherDeliveryRuleProvinces();
		return "jsonOtherDeliveryRuleProvinces";
	}

	/**
	 * 依城市查询大材积支援的地区
	 * 
	 * @param city
	 * @return
	 * @throws CoreException
	 */
	public String findOtherDeliveryRuleDistrictsByCity() throws Exception {
		districts = deliverRuleService.findOtherDeliveryRuleDistrictsByCity(city);
		return "jsonOtherDeliveryRuleDistrictsByCity";
	}

	/**
	 * 依省份查询大材积支援的城市
	 * 
	 * @param province
	 * @return
	 * @throws CoreException
	 */
	public String findOtherDeliveryRuleCitysByProvince() throws Exception {
		citys = deliverRuleService.findOtherDeliveryRuleCitysByProvince(province);
		return "jsonOtherDeliveryRuleCitysByProvince";
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public String getActionResult() {
		return actionResult;
	}

	public void setActionResult(String actionResult) {
		this.actionResult = actionResult;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public String getService() {
		return service;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setService(String service) {
		this.service = service;
	}

	public Object[] getProvinces() {
		return provinces;
	}

	public void setProvinces(Object[] provinces) {
		this.provinces = provinces;
	}

	public Object[] getCitys() {
		return citys;
	}

	public Object[] getDistricts() {
		return districts;
	}

	public void setCitys(Object[] citys) {
		this.citys = citys;
	}

	public void setDistricts(Object[] districts) {
		this.districts = districts;
	}
}